<template>
  <scroll-view
    class="content"
    scroll-y
    :style="{ height: widHeigh + 'px' }"
    lower-threshold="60"
    @scrolltolower="onreachBottom"
  >
    <view class="exhibit" v-if="productList.length != 0">
      <view
        class="exhibit_item"
        v-for="(item, index) in productList"
        :key="item.id"
        @click="toGoodsDetail(item.id)"
      >
        <view class="exhibit_item_wrapper">
          <view class="exhibit_item_timer">
            <image
              class="exhibit_item_timerIcon"
              src="../../static/index_timer.png"
            ></image>

            <view class="exhibit_item_timerText">
              <text
                class="exhibit_item_sale1"
                v-if="is_Coming_soon(item.opening_at)"
                >即将开售</text
              >
              <text class="exhibit_item_sale2">{{ item.opening_at }} 开售</text>
            </view>
          </view>

          <image class="exhibit_item_goodsImage" :src="item.cover"></image>
        </view>

        <view class="exhibit_item_bottom">
          <view class="exhibit_item_left">
            <view class="exhibit_item_first">{{ item.name }}</view>
            <view class="exhibit_item_second">
              <view class="exhibit_item_limited">限量</view>
              <view class="exhibit_item_inStock">{{ item.total_num }}份</view>
            </view>
            <view class="exhibit_item_third">
              <image
                class="exhibit_item_avatar"
                :src="item.creator_avatar"
              ></image>
              <view class="exhibit_item_username">{{ item.creator_name }}</view>
            </view>
          </view>
          <view class="exhibit_item_right">
            <!-- @click.stop="handleCreateOrder(item.id, item.opening_at)" -->
            <!-- <view class="exhibit_item_button" v-if="item.type == 1">秒杀</view> -->
            <template v-if="item.type == 1">
              <!-- 收藏列表多了一层 product -->
              <view
                v-if="
                  userInfo &&
                  userInfo.is_frist_buy == 1 &&
                  item.first_buy_at &&
                  !is_Coming_soon(item.first_buy_at)
                "
                class="exhibit_item_button"
                >秒杀</view
              >
              <view
                v-else
                class="exhibit_item_button"
                :class="
                  is_Coming_soon(item.opening_at)
                    ? 'exhibit_item_buttonGray'
                    : ''
                "
                >秒杀</view
              >
            </template>

            <!-- @click="handleReservedApply(item.id)" -->
            <template v-else>
              <view
                v-if="!is_Coming_soon(item.opening_at)"
                class="exhibit_item_buttonGray"
                >已结束</view
              >

              <view
                v-else-if="
                  item.is_reserved_apply == 1 && is_Coming_soon(item.opening_at)
                "
                class="exhibit_item_buttonGray"
                >已预约</view
              >

              <view v-else class="exhibit_item_button">预约</view>
            </template>

            <view class="exhibit_item_price">¥ {{ item.price }}</view>
          </view>
        </view>
      </view>
    </view>

    <empty :show.sync="productList.length == 0 && pagestate"></empty>
  </scroll-view>
</template>

<script>
import api from "@/api/api.js";
import tip from "@/utils/tip.js";
import moment from "moment";
import { isLogin } from "@/utils/auth.js";
import empty from "@/components/empty/empty";
export default {
  components: {
    empty,
  },
  data() {
    return {
      widHeigh: 0,

      current: 0,

      swiper_list: [],
      swiperIndex: "",

      page: 1,
      limit: 10,
      productList: [],
      pagestate: false,
      is_empty: false,

      userInfo: null,
    };
  },
  onLoad() {
    let windowElement = uni.getSystemInfoSync();
    this.widHeigh = windowElement.windowHeight;
  },
  onShow() {
    this.getcollection(true);
    this.getUserInfo();
  },
  computed: {
    is_Coming_soon() {
      return (time) => {
        return moment(time).diff(moment(), "second") > 0;
      };
    },
  },
  methods: {
    async getcollection(pagestate) {
      if (pagestate) {
        this.page = 1;
      }
      let res = await api.CollectLog({
        query: {
          page: this.page,
          limit: this.limit,
        },
      });
      console.log(res, "res");
      console.log(this.page, "pagepage");
      // this.productList = res.data;
      if (this.page == 1) {
        this.productList = [];
      }
      this.pagestate = false;
      for (var i = 0; i < res.data.length; i++) {
        console.log("执行了");
        this.productList.push(res.data[i].product);
      }
      this.pagestate = !this.pagestate;

      console.log(this.productList);
    },
    // Swiper改变时获取到下标
    swiperChange(index) {
      this.swiperIndex = index;
    },
    maskTouchend() {
      // uni.navigateTo({
      //   url: `../../pages/collect_record/collect_record`,
      // });
    },

    async getUserInfo() {
      let res = await api.info({
        method: "get",
        query: {},
      });
      if (res.code === 0) {
        this.userInfo = res.data;
      } else {
        tip.defaults(res.msg);
      }
    },

    async getProductList(refresh) {
      if (refresh) {
        this.page = 1;
      }
      let res = await api.productList({
        method: "get",
        query: {
          page: this.page,
          limit: this.limit,
          keyword: "",
        },
      });
      if (res.code === 0) {
        if (refresh) {
          this.productList = res.data;
        } else {
          this.productList = this.productList.concat(res.data);
        }
      } else {
        tip.defaults(res.msg);
      }
    },

    async handleCreateOrder(product_id, time) {
      // if (moment(time).diff(moment(), "second") > 0) {
      //   tip.defaults("未到开售时间");
      //   return;
      // }
      // const res = await api.productCreateOrder({
      //   method: "post",
      //   query: {
      //     product_id: product_id,
      //   },
      // });
      // if (res.code == 0) {
      //   this.order_no = res.data;
      //   if (res.data) {
      //     this.pay();
      //   }
      // } else {
      //   tip.defaults(res.msg);
      // }
    },

    async pay() {
      const res = await api.pay({
        method: "post",
        query: {
          pay_type: 3,
          order_no: this.order_no,
        },
      });

      if (res.code == 0) {
        var params = res.data;
        // App上生效
        let orderInfo = {
          appid: params.appid, //对应的appid
          noncestr: params.noncestr,
          package: params.package,
          partnerid: params.partnerid,
          prepayid: params.prepayid,
          timestamp: params.timestamp,
          sign: params.sign, //二次签名
        };
        uni.requestPayment({
          provider: "wxpay", // wxpay、alipay
          orderInfo: JSON.stringify(orderInfo), //微信、支付宝订单数据
          success: function (res) {
            tip.success("支付成功");
            console.log("res", res);
          },
          fail: function (err) {
            tip.defaults("支付失败");
            console.log("err", err);
          },
        });
      } else {
        tip.defaults(res.msg);
      }
    },

    // 预约
    async handleReservedApply(product_id) {
      // const res = await api.reservedApply({
      //   method: "post",
      //   query: {
      //     product_id: product_id,
      //   },
      // });
      // if (res.code == 0) {
      //   tip.defaults("预约成功");
      // } else {
      //   tip.defaults(res.msg);
      // }
    },

    tabsChange(index) {
      this.current = index;
    },
    async toGoodsDetail(product_id) {
      // await isLogin();
      uni.navigateTo({
        url: `../../pages/goods_detail/goods_detail?product_id=${product_id}`,
      });
    },

    onreachBottom() {
      console.log("触发 onreachBottom");
      this.page++;
      this.$u.throttle(this.getcollection(), 1000, true);
    },
  },
};
</script>

<style>
page {
  background-color: #13111b;
}
</style>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.content {
  width: 690rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  padding: 0 30rpx 0 30rpx;
  background: #13111b;

  // background-image: url(../../static/index_bg.png);
  // background-repeat: no-repeat;
  // background-size: 100% 510rpx;
}

.search {
  width: 690rpx;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  &_logo {
    width: 192rpx;
    height: 54rpx;
  }
  &_icon {
    width: 40rpx;
    height: 40rpx;
  }
}
.top {
  width: 690rpx;
  height: 280rpx;
  background: black;
  border-radius: 24rpx;
  &_image1 {
    width: 690rpx;
    height: 280rpx;
  }
}
.u-tabs_line {
  display: flex;
  position: relative;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .u-tabs_wrapper {
    width: 70%;
  }
}
.exhibit {
  overflow: hidden;
  &_item {
    width: 690rpx;
    height: 904rpx;
    background: #1d1d28;
    border-radius: 36rpx;
    &_wrapper {
      position: relative;
      width: 690rpx;
      height: 690rpx;
    }
    &_timer {
      z-index: 1;
      position: absolute;
      top: 26rpx;
      left: 26rpx;

      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20rpx;
      // width: 408rpx;
      height: 60rpx;
      background: #000000;
      border-radius: 30rpx;
      // opacity: 0.46;
      background-color: rgba(0, 0, 0, 0.46);
    }
    &_timerIcon {
      width: 32rpx;
      height: 32rpx;
    }
    &_timerText {
      margin-left: 8rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #f78623;
      line-height: 40rpx;
      // opacity: 0.46;
    }
    &_sale1 {
    }
    &_sale2 {
      margin-left: 12rpx;
    }
    &_goodsImage {
      border-radius: 36rpx;
      width: 690rpx;
      height: 690rpx;
    }
    &_bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    &_left {
      margin: 30rpx 0 0 30rpx;
    }
    &_right {
      height: 138rpx;
      margin: 46rpx 30rpx 0 0;
    }
    &_first {
      width: 400rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 44rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    &_second {
      display: flex;
      align-items: center;

      margin: 20rpx 0;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;

      // width: 202rpx;
      height: 34rpx;
      border-radius: 4rpx;
    }
    &_limited {
      // width: 76rpx;
      padding: 0 14rpx;
      height: 34rpx;
      background: linear-gradient(130deg, #f78623 0%, #d5280a 100%);
      border-radius: 4rpx;
      text-align: center;
    }
    &_inStock {
      padding: 0 14rpx;
      // margin-left: 10rpx;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;
      background: #262934;
    }
    &_third {
      display: flex;
      align-items: center;

      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_avatar {
      border-radius: 50%;
      margin-right: 8rpx;
      width: 36rpx;
      height: 36rpx;
    }
    &_username {
      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_button {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      line-height: 56rpx;
      color: $text-color4;
      background: $button;background-repeat: no-repeat;background-size: 100%;
    }
    &_buttonGray {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      background: #999999;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 56rpx;
    }
    &_price {
      margin-top: 34rpx;
      font-size: 40rpx;
      font-family: SFPro-Bold, SFPro;
      font-weight: bold;
      color: #ffffff;
      line-height: 48rpx;
    }
  }
  &_item:not(:first-child) {
    margin-top: 30rpx;
  }
  &_item:last-child {
    margin-bottom: 30rpx;
  }
}
</style>
